/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.zhuike.novel.app.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.BooksByCats;
import net.zhuike.novel.app.bean.Rankings;
import net.zhuike.novel.app.bean.Recommend;
import net.zhuike.novel.app.manager.CollectionsManager;
import net.zhuike.novel.app.manager.SettingManager;
import net.zhuike.novel.app.ui.activity.BookDetailActivity;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.FileUtils;
import net.zhuike.novel.app.ui.utils.FormatUtils;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;
import net.zhuike.novel.app.ui.utils.ToastUtils;

import java.text.NumberFormat;
import java.util.List;

/**
 * @author wq.
 * @date 2017/6/13.
 */
public class SubRankAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater flater;
    public List<Rankings.BooksBean>list;
    private Recommend.RecommendBooks recommendBooks;
    private boolean isJoinedCollections = false;
    public SubRankAdapter(Context context, List<Rankings.BooksBean> list) {
        this.context = context;
        this.list = list;
        this.flater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = flater.inflate(R.layout.item_sub_rank_list, null);
            holder.tvSubRankTitle = (TextView) convertView
                    .findViewById(R.id.tvSubRankTitle);
            holder.tvSubCateMsg = (TextView) convertView
                    .findViewById(R.id.tvSubCateMsg);
            holder.tvSubCateAuthor = (TextView) convertView
                    .findViewById(R.id.tvSubCateAuthor);
            holder.ivSubRankCover= (ImageView) convertView
                    .findViewById(R.id.ivSubRankCover);
            holder.addShlefBtn=(ImageView) convertView
                    .findViewById(R.id.addShlefBtn);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Rankings.BooksBean item=list.get(position);
        holder.tvSubRankTitle.setText(item.name);
        holder.tvSubCateAuthor.setText(item.author);
        holder.tvSubCateMsg.setText(Html.fromHtml(item.description));
        if (item.cover != null ) {
            Glide.with(context).load( item.cover).placeholder(R.mipmap.cover_default).error(R.mipmap.cover_default)
                    .into(holder.ivSubRankCover);
        }

        return convertView;
    }

    static class ViewHolder {
        public TextView tvSubRankTitle;
        public TextView tvSubCateMsg;
        public TextView tvSubCateAuthor;
        public ImageView ivSubRankCover;
        public ImageView addShlefBtn;
    }
    private void initCollection(boolean coll,TextView view) {
        if (coll) {
            view.setBackgroundResource(R.drawable.shape_bg_btn);
            isJoinedCollections = false;
        } else {
            view.setBackgroundResource(R.drawable.shape_bg_press_btn);
            isJoinedCollections = true;
            view.setText("已加入书架");
        }
    }
}
